package com.qxzn.app.page.robot;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;

import com.qxzn.app.page.view.MyDialog;
import com.qxzn.app.util.SystemUtil;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.Arrays;
import java.util.List;

import io.reactivex.functions.Consumer;

public class PermissionHelper {

    public interface OnDoneListener {
        void onDone();
    }

    public static void checkCameraPermission(@NonNull FragmentActivity activity, OnDoneListener listener) {
        boolean needCheckPermission = false;
        List<String> permissions = Arrays.asList(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA);
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
                needCheckPermission = true;
                break;
            }
        }
        if (needCheckPermission) {
            showDialog(activity, "“奇信康派”想访问您的照片", "只有开启相机权限才能正常使用拍照和扫码功能",
                    "不允许",
                    "好",
                    (dialog, which) -> dialog.dismiss(),
                    (dialog, which) -> {
                        dialog.dismiss();
                        new RxPermissions(activity).request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA).subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(Boolean aBoolean) throws Exception {
                                if (aBoolean) {
                                    if (listener != null) listener.onDone();
                                } else {
                                    showDialog(activity, "奇信康派",
                                            "该功能需要存储权限和相机权限",
                                            "下次再说",
                                            "去授权",
                                            (dialog0, which0) -> dialog0.dismiss(),
                                            (dialog1, which1) -> {
                                                SystemUtil.toPermissionSetting(activity);
                                                dialog1.dismiss();
                                            });
                                }
                            }
                        });
                    });
        } else if (listener != null) listener.onDone();
    }

    public static void startActivity(FragmentActivity activity, Class activityClass) {
        checkCameraPermission(activity, () -> activity.startActivity(new Intent(activity, activityClass)));
    }


    private static void showDialog(Activity activity, String title, String message, String negativeText, String positiveText, final DialogInterface.OnClickListener negativeListener, final DialogInterface.OnClickListener positiveListener) {
        new MyDialog.Builder(activity)
                .setTitle(title)
                .setMessage(message)
                .setNegativeButtonText(negativeText)
                .setPositiveButtonText(positiveText)
                .setNegativeButton((dialog, which) -> {
                    if (negativeListener != null) {
                        negativeListener.onClick(dialog, which);
                    }
                    dialog.dismiss();
                })
                .setPositiveButton((dialog, which) -> {
                    if (positiveListener != null) {
                        positiveListener.onClick(dialog, which);
                    }
                    dialog.dismiss();
                })
                .setCancelAble(false)
                .create()
                .show();
    }

}
